<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        const arr1 = [9, 9, 9]
        const arr2 = [4, 3, 2, 1]
        const arr3 = [9];
        const newarr = [];
        function plusOne(arr) {
            for (let i = arr.length - 1; i >= 0; i--) {
                item = arr[i]

                if (item < 9) {
                    item++;
                    newarr.push(item);
                    arr.splice(i, 1)
                    for (let k = arr.length - 1; k >= 0; k--) {
                        newarr.unshift(arr[k])
                    }
                    return newarr
                }
                else if (item >= 9) {
                    item = 0;
                    newarr.unshift(item)
                    arr.splice(i, 1)
                    plusOne(arr);
                    return newarr
                }

            }
            if (newarr[0] == 0) {
                return newarr.unshift(1);
            }
        };
        // console.log(plusOne(arr1));
        console.log(plusOne(arr2));

    </script>
</body>

</html>